package fx.iog.home;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import fx.iog.gen.R;

public class HomeActivity extends Activity {

	private DrawerLayout layoutDrawer;

	private ListView listDrawer;

	private ActionBarDrawerToggle drawerToggle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_home);

		// final ActionBar actionBar = getActionBar();

		// actionBar.setDisplayShowTitleEnabled(false);
		// actionBar.setDisplayShowHomeEnabled(false);

		layoutDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

		listDrawer = (ListView) findViewById(R.id.drawer_list);

		drawerToggle = new ActionBarDrawerToggle(this, layoutDrawer,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {

			/** Called when drawer is closed */
			public void onDrawerClosed(View view) {
				getActionBar().setTitle("Eventos");
				invalidateOptionsMenu();
			}

			/** Called when a drawer is opened */
			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle("Jorginho Bazaga");
				invalidateOptionsMenu();
			}
		};

		layoutDrawer.setDrawerListener(drawerToggle);

		// Creating an ArrayAdapter to add items to the listview mDrawerList
		// ArrayAdapter<String> adapter = new
		// ArrayAdapter<String>(getBaseContext(),
		// R.layout.drawer_list_item,
		// getResources().getStringArray(R.array.menus));

		MenuDrawerAdapter adapter = new MenuDrawerAdapter(this,
				R.array.img_menudrawer, R.array.txt_menudrawer);

		// Setting the adapter on mDrawerList
		listDrawer.setAdapter(adapter);

		// Enabling Home button
		getActionBar().setHomeButtonEnabled(true);

		// Enabling Up navigation
		getActionBar().setDisplayHomeAsUpEnabled(true);

		// Setting item click listener for the listview mDrawerList
		listDrawer.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// Getting an array of rivers
				String[] menuItems = getResources().getStringArray(
						R.array.txt_menudrawer);

				// Currently selected river
				// mTitle = menuItems[position];

				// Creating a fragment object
				// WebViewFragment rFragment = new WebViewFragment();

				// Passing selected item information to fragment

				// rFragment.setArguments(data);

				// Getting reference to the FragmentManager
				FragmentManager fragmentManager = getFragmentManager();

				// Creating a fragment transaction
				FragmentTransaction ft = fragmentManager.beginTransaction();

				// Adding a fragment to the fragment transaction
				// ft.replace(R.id.content_frame, rFragment);

				// Committing the transaction
				ft.commit();

				// Closing the drawer
				layoutDrawer.closeDrawer(listDrawer);

			}
		});
		
		HomeFragment homeFrag = new HomeFragment();
		

		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();

		//transaction.setCustomAnimations(R.anim.fragment_animation_fade_in,
				//R.anim.fragment_animation_fade_out);

		transaction.add(R.id.content_frame, homeFrag, "home");

		transaction.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);

		SearchView sv = (SearchView) menu.findItem(R.id.act_search)
				.getActionView();

		sv.setOnQueryTextListener(new SearchFilter());

		int magId = getResources().getIdentifier("android:id/search_mag_icon",
				null, null);
		ImageView magImage = (ImageView) sv.findViewById(magId);
		magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));

		sv.setIconifiedByDefault(false);

		magImage.setVisibility(View.GONE);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		drawerToggle.syncState();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (drawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void teste(MenuItem item) {
		System.out.println("OLSDOADOASODSAODSOADOSADOAODAODSAODOAS");

	}

}
